package utils;

import pojo.FileItem;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;

import static utils.FileUtils.*;

/**
 * 客户端使用的网络通信工具类V2
 */
public class NetworkUtilsV2 {
    /**
     * 登录服务器方法
     * @param username
     * @param password
     * @return
     */
    public static Long login(String username,String password) {
        //连接服务器
        try(Socket socket=new Socket(IP,PORT);
            DataInputStream inputStream=new DataInputStream(socket.getInputStream());
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream())){
            //发送登录命令
            dataOutputStream.writeInt(TYPE_LOGIN);
            //发送账号
            dataOutputStream.writeUTF(username);
            //发送密码
            dataOutputStream.writeUTF(password);
            //接受用户Id
            long userId = inputStream.readLong();
            return userId;
        }catch (UnknownHostException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 获得服务器的文件列表
     * @return
     */
    public static List<FileItem> getFileItems(Long userId){
        try(Socket socket=new Socket(IP,PORT);
            DataInputStream inputStream=new DataInputStream(socket.getInputStream());
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream())){
            //发送查询命令
            dataOutputStream.writeInt(TYPE_LIST);
            //发送用户ID
            dataOutputStream.writeLong(userId);
            //接收服务器发送的JSON
            String s = inputStream.readUTF();
            //解析JSON,然后返回集合
            return jsonToList(s);

        }catch (IOException e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 文件上传
     *
     */
    public static void uploadFile(Long userId,String path){
        try(Socket socket=new Socket(IP,PORT);
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream())){
            //发送查询命令
            dataOutputStream.writeInt(TYPE_UPLOAD);
            final File file = new File(path);
            if(! file.exists()){
                dataOutputStream.writeUTF("error");
                System.out.println("文件不存在");
                return;
            }
            //发送文件名
            dataOutputStream.writeUTF(file.getName());
            //发送用户ID
            dataOutputStream.writeLong(userId);
            //创建文件输入流
            FileInputStream fileInputStream = new FileInputStream(path);
            //读取文件流，发送给服务器
            FileUtils.io(fileInputStream,dataOutputStream);
            System.out.println("上传完毕");
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    /**
     * 文件下载
     */
    public static void downloadFile(Long fileId){
        try(Socket socket=new Socket(IP,PORT);
            DataInputStream inputStream = new DataInputStream(socket.getInputStream());
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream())
            ) {
            //发送下载命令
            dataOutputStream.writeInt(TYPE_DOWNLOAD);
            //发送文件名
            dataOutputStream.writeLong(fileId);
            //接受服务器端的消息
            String name = inputStream.readUTF();
            if ("error".equals(name)){
                System.out.println("服务器不存在该文件");
                return;
            }
            //判断下载目录如果不存在就创建
            File file = new File(DOWNLOAD_DIR);
            if(!file.exists()){
                file.mkdirs();
            }
            //读取服务器端的数据写入磁盘文件
            FileOutputStream fileOutputStream = new FileOutputStream(DOWNLOAD_DIR + "\\" + name);
            FileUtils.io(inputStream,fileOutputStream);
            System.out.println("下载完成");
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 文件删除
     */

    public static void deleteFiles(Long fileId){
        try(Socket socket=new Socket(IP,PORT);
            DataInputStream inputStream = new DataInputStream(socket.getInputStream());
            DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream())
        ) {
            //发送删除命令
            dataOutputStream.writeInt(TYPE_DELETE);
            //发送文件名
            dataOutputStream.writeLong(fileId);
            //接受服务器端的消息
            String name = inputStream.readUTF();
            if ("error".equals(name)){
                System.out.println("服务器不存在该文件");
                return;
            }
            System.out.println("删除完成");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
